require('../../assets/css/index.js')
require('../../assets/fonts/font/iconfont.css')
require('../../assets/lib/swiper/swiper-bundle.css')
let Swiper = require('../../assets/lib/swiper/swiper-bundle.js')
require('../../utils/local.js')
require('../../assets/lib/weui/weui.css')
// 引入公共js
let dom = require('../../utils/dom.js')
const local = require('../../utils/local.js')
let weui = require('../../assets/lib/weui/weui.js')
const { default: axios } = require('axios')
let https = require('../../utils/https.js')


//引入当前页面的less文件
require('./myself.less')
// 当前页面js
dom.isLogin()

dom.ready(function () {
    dom.getEle('.return').addEventListener("click", function () {
        window.history.go(-1);
    });
    // 切换头像
    let imgUrl;
    dom.getEle('#picBox').addEventListener("change", function () {
        // console.dir(this.files[0]);
        let pic = this.files[0];

        // 拼接后端所需上传文件格式
        let fd = new FormData();
        fd.append('file', pic);//固定拼接方式

        // https.upLoadPic('http://47.96.154.185:3701/api/shared/uploadPortrait1','file',this.files[0],function(res){
        //     if(res.errno==0){
        //         dom.getEle('#headPic').src=https.hostUrl+res.data.url; 
        //     }
        // })
        //    获取上传图像

        // 发送数据到后台
        console.log(fd.get('file'));
        https.post('/api/shared/uploadPortrait1',fd,function(res){
            // console.log(res);
            if (res.errno == 0) {
                dom.getEle('#headPic').src = https.hostUrl + res.data.url;
                imgUrl=res.data.url;
            }
        })
        // axios.post('http://47.96.154.185:3701/api/shared/uploadPortrait1', fd).then(res => {
        //     console.log(res.data);
        //     // 换头像
        //     if (res.data.errno == 0) {
        //         dom.getEle('#headPic').src = https.hostUrl + res.data.data.url;
        //         imgUrl=res.data.data.url;
        //     }
        // }).catch(err => {
        //     console.log(err);
        // })


    })
    // 性别选择
    let gender;
    dom.getEle('#sex').addEventListener("click", function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }, {
            label: '未知',
            value: 2
        },], {
            onChange: function (result) {
                console.log('改变事件', result);
            },
            onConfirm: function (result) {
                dom.getEle('#sexTxt').innerHTML = result[0].label;
                gender=result[0].value;

            },
            title: '请选择性别'
        });
    })
    // 省份选择
    let provinceId;
    dom.getEle('#province').addEventListener("click", function () {
        https.get('/api/shared/province', {}, function (res) {
            // console.log(res);
            if (res.errno == 0) {
                let provdata = res.data.map(v => {
                    // console.log(v);
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                weui.picker(provdata, {
                    onConfirm: function (result) {
                        console.log('确认事件', result);
                        dom.getEle('#provinceTxt').innerHTML = result[0].label;
                        console.log(result[0].value);
                        provinceId = result[0].value;
                    },
                    title: '请选择省份'
                })
            }


        });
    })
    //城市选择
    let cityId;
    dom.getEle('#city').addEventListener("click", function () {
        if (provinceId) {
            https.get('/api/shared/city?provinceId=' + provinceId, {}, function (res) {
                if (res.errno == 0) {
                    let provdata = res.data.map(v => {
                        // console.log(v);
                        return {
                            label: v.name,
                            value: v.id
                        }
                    })
                    weui.picker(provdata, {
                        onConfirm: function (result) {
                            dom.getEle('#cityTxt').innerHTML = result[0].label;
                            cityId=result[0].value
                        },
                        title: '请选择城市'
                    })
                }


            });
        }

    })
    // 生日选择
    let birthday;
    dom.getEle('#birthday').addEventListener("click", function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                let birthday = result.map(function (v) {
                    return v.value < 10 ? '0' + v.value : v.value;
                }).join('-');
                dom.getEle('#birthdayTxt').innerHTML = birthday;
                // dom.getEle('#birthdayTxt').innerHTML = result[0].label + result[1].label + result[2].label;
            },
            title: '请选择生日'
        });
    })
    // 监听输入
    dom.getEle('#writeText').addEventListener('input', function () {
        if(this.value.length>200){
            dom.getEle('#writeText').value=(this.value).slice(0,200)
        }
        dom.getEle('#write').innerHTML = dom.getEle('#writeText').value.length
    })
    // 保存
    dom.getEle('#btn').addEventListener('click', function () {
        // 获取用户所有数据,考虑用户没有修改情况
        let userData={
            nickName:nickTxt.value,

            gender:gender,

            birthday:birthday,

            imgUrl:imgUrl,

            provinceId:provinceId,

            cityId:cityId,

            sign:writeText.value,


        }
        // 发送请求
        https.post('/api/user/changeInfo',userData,function(res){
            console.log(res);
           
            if(res.errno==0){
                localStorage.setItem('tk',res.data.token),
                // localStorage.setItem('userData',res)
                window.location.href='mine.html'

            }
        })

    })
    // 回填信息
    function rander(){
        let provinceId;
        let localData=local.get('userInfo')
        dom.getEle('#headPic').src=https.hostUrl+localData.imgUrl;
        imgUrl=localData.imgUrl;
        dom.getEle('#nickTxt').value=localData.nickName;
        dom.getEle('#sexTxt').innerHTML=localData.gender==0?'女':(localData.gender==1?'男':'未知')
        gender=localData.gender;
        dom.getEle('#writeText').value=localData.sign;

        dom.getEle('#birthdayTxt').innerHTML=localData.birthday;
        birthday=localData.birthday;
        //省份和城市
        https.get('/api/shared/province',{},function(res){

            if(res.errno==0){
                res.data.map(v=>{
       

                    // 判定当前用户的省份
                    if(v.id==localData.provinceId){
                        
                        provinceId=v.id;
                        dom.getEle('#provinceTxt').innerHTML=v.name;
                        https.get('/api/shared/city',{provinceId},function(res){
            
                            if(res.errno==0){
                                res.data.map(v=>{
                                    // 判定当前用户的省份
                                    if(v.id==localData.cityId){
                                        cityId=v.id;
                                        dom.getEle('#cityTxt').innerHTML=v.name;
                                    }
                                })  
                            }
                        })
                    }
                })
            }
        })
       
        
    }
    rander()

})